//
//  AvoidTheCircleGameScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/9/13.
//
//

#include "AvoidTheCircleGameScene.h"
#include "GameLayerManager.h"
#include "SceneManager.h"
#include "MyUtilities.h"
#include "MyBouncingButton.h"
CCScene* AvoidTheCircleGameScene::scene(){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    AvoidTheCircleGameScene *layer = AvoidTheCircleGameScene::create();
    GameLayerManager::sharedInstance()->avoidTheCircleGameLayer = layer;
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

bool AvoidTheCircleGameScene::init(){
    CountTimeGameScene::init();
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonWidth = winSize.width * 0.3f;
    
    button = MySpriteButton::create("circle_button.png", (winSize.width - buttonWidth)/2, ((winSize.height - scoreAndTimeLayer->getBarHeight()-buttonWidth)/2), buttonWidth, buttonWidth);
    CCCallFuncO* callBackOnTap = CCCallFuncO::create(this, callfuncO_selector(AvoidTheCircleGameScene::onTapTheButton), this);
    button->addCallback(callBackOnTap);
    this->addChild(button,1);
    originalPosition = button->getPosition();
    superStartPoint = button->getPosition();
    
    bouncingArray = CCArray::create();
    bouncingArray->retain();
    
    totalButtonArray = CCArray::create();
    totalButtonArray->retain();
    totalButtonArray->addObject(button);


    return true;
}

void AvoidTheCircleGameScene::randomlyCreateBouncingCircles(int numberOfCircles){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.1f;
    int maxX = winSize.width - buttonSize;
    int maxY = winSize.height - scoreAndTimeLayer->getBarHeight() - buttonSize;
    
    
    for(int i=0;i<numberOfCircles;i++){
        int posX;
        int posY;
        CCRect iRect;
        bool hasIntersectSomeWhere;
        do {
            hasIntersectSomeWhere = false;
            posX = rand()%maxX;
            posY = rand()%maxY;
            iRect = CCRectMake(posX, posY, buttonSize, buttonSize);
            
            for(int j=0;j<totalButtonArray->count();j++){
                MyButton* button = (MyButton*)totalButtonArray->objectAtIndex(j);
                if(button->getBoundingRect().intersectsRect(iRect)){
                    hasIntersectSomeWhere = true;
                    break;
                }
            }
            
        } while (hasIntersectSomeWhere);
        MyBouncingButton* iButton = MyBouncingButton::create("circle_button.png", iRect.origin.x, iRect.origin.y, iRect.size.width, iRect.size.height);
        iButton->addLimitRect(CCRectMake(0, 0, winSize.width, winSize.height-scoreAndTimeLayer->getBarHeight()));
        this->addChild(iButton);
        
        bool thisDirectionIsUnique;
        do {
            thisDirectionIsUnique = true;
            iButton->pushToMoveToRandomDirection();
            for(int j=0;j<bouncingArray->count();j++){
                MyBouncingButton* jButton = (MyBouncingButton*)bouncingArray->objectAtIndex(j);
                if(jButton->isOnTheSameDiretionWith(iButton)){
                    thisDirectionIsUnique = false;
                    break;
                }
            }
        } while (thisDirectionIsUnique==false);
        
        
        
        totalButtonArray->addObject(iButton);
        bouncingArray->addObject(iButton);
        
    }
}

void AvoidTheCircleGameScene::onTapTheButton(cocos2d::CCObject *sender){
    //gameOver();
}

void AvoidTheCircleGameScene::update(float dt){
    for(int i=0;i<bouncingArray->count();i++){
        MyBouncingButton* iButton = (MyBouncingButton*)bouncingArray->objectAtIndex(i);
        iButton->handleUpdate(dt);
        if(MyUtilities::circleIntersectCircle(iButton->getBoundingRect(), button->getBoundingRect())){
            gameOver();
            break;
        }
    }
}
void AvoidTheCircleGameScene::onEnter(){
    CountTimeGameScene::onEnter();
    randomlyCreateBouncingCircles(3);
}

void AvoidTheCircleGameScene::startGame(){
    CountTimeGameScene::startGame();
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->avoidTheCircleGameScene;


    this->scheduleUpdate();
    
}
void AvoidTheCircleGameScene::reset(){
    CountTimeGameScene::reset();
    button->setPosition(superStartPoint);
    
    for(int i=0;i<bouncingArray->count();i++){
        MyBouncingButton* iButton = (MyBouncingButton*)bouncingArray->objectAtIndex(i);
        iButton->removeFromParent();
        totalButtonArray->removeObject(iButton);
    }
    bouncingArray->removeAllObjects();
    button->resetState();
    

}
void AvoidTheCircleGameScene::onTick(float dt){
    CountTimeGameScene::onTick(dt);
    for(int i=0;i<bouncingArray->count();i++){
        MyBouncingButton* iButton = (MyBouncingButton*)bouncingArray->objectAtIndex(i);
        iButton->goFaster();
    }
}

void AvoidTheCircleGameScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    button->handleTouchesBegan(touches, pEvent);
    if(button->isFocused){
        originalPosition = button->getPosition();
    }
    beginPoint = MyUtilities::getCoCosPointFromTouches(touches);
    
}

void AvoidTheCircleGameScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if(button->isFocused){
        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        CCPoint touchPosition = MyUtilities::getCoCosPointFromTouches(touches);
        double distanceX = touchPosition.x - beginPoint.x;
        double distanceY = touchPosition.y - beginPoint.y;
        button->setPosition(ccp(originalPosition.x+distanceX,originalPosition.y+distanceY));
        if(button->getPositionX()<0){
            button->setPosition(ccp(0,button->getPositionY()));
        }
        if(button->getPositionX()>winSize.width-button->getBoundingRect().size.width){
            button->setPosition(ccp(winSize.width-button->getBoundingRect().size.width,button->getPositionY()));
        }
        if(button->getPositionY()<0){
            button->setPosition(ccp(button->getPositionX(),0));
        }
        if(button->getPositionY()>winSize.height-scoreAndTimeLayer->getBarHeight()-button->getBoundingRect().size.height){
            button->setPosition(ccp(button->getPositionX(),winSize.height-scoreAndTimeLayer->getBarHeight()-button->getBoundingRect().size.height));
        }
    }
}

void AvoidTheCircleGameScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    button->handleTouchesEnded(touches, pEvent);
}